
import os, _pg
import svn

def setupTables(db):
    q = """
CREATE TABLE RepUsers(
    RepID       serial      NOT NULL,           
    PersonID    int         references People(PersonID),
    RepName     varchar(150)    NOT NULL,
PRIMARY KEY  (RepID)
);
"""
    dropq = "drop table RepUsers cascade"
    try:
        print "cleaning dev table..."
        db.query(dropq)
        print "...OK!"
    except _pg.ProgrammingError, e:
        print "could not drop tables:", str(e)
    try:
        print "creating dev table..."
        db.query(q)
        print "...OK!"
    except _pg.ProgrammingError, e:
        print "could not create tables:", str(e)
    
def start(proj, log):
    """store the svn users from the repolog into the database"""
    db = proj.getDatabase()
    svn = proj.getSVNRepository()
    setupTables(db)
    svn.buildRepUsers(db, log) 
    log.write("built and populated RepUsers table")
